/* main.c */

#include "app_inc.h"
#include "fsl_edma.h"


uint8_t SrcBuf[10] = {0,1,2,3,4,5,6,7,8,9};
uint8_t DestBuf[10];

int main(void)
{
    uint32_t i;
    
    /* Initialize the board. */
    BSP_InitSystem();
    BSP_InitStdioUART(115200);
    
    printf("\r\nDMA Memory-Memory test\r\n");

    for(i=0;i<sizeof(SrcBuf);i++)
    {
        SrcBuf[i] = i & 0xFF;
        DestBuf[i] = 0;
    }

    
    /* enable DMA and DMAMUX clock */
    SIM->SCGC6 |= SIM_SCGC6_DMAMUX_MASK;
    SIM->SCGC7 |= SIM_SCGC7_DMA_MASK;

    /* configure DMAMUX */
    DMAMUX->CHCFG[0]= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(63);

    /* source configuration */
    DMA0->TCD[0].SADDR = (uint32_t)SrcBuf; 
    DMA0->TCD[0].ATTR = DMA_ATTR_SSIZE(0);
    DMA0->TCD[0].SOFF= 1; 
    DMA0->TCD[0].SLAST = 0;

    /* destination configuration */
    DMA0->TCD[0].DADDR = (uint32_t)DestBuf;
    DMA0->TCD[0].ATTR = DMA_ATTR_DSIZE(0);
    DMA0->TCD[0].DOFF= 1;
    DMA0->TCD[0].DLAST_SGA= 0;
    /* set CITER and BITER to maximum value */
    DMA0->TCD[0].CITER_ELINKNO = sizeof(SrcBuf); 
    DMA0->TCD[0].BITER_ELINKNO = sizeof(SrcBuf);
    DMA0->TCD[0].NBYTES_MLNO = 1; 

    /* enable auto disable request */
    DMA0->TCD[0].CSR |= DMA_CSR_DREQ_MASK; 

    /* enable interrupt enable */
    DMA0->TCD[0].CSR |= DMA_CSR_INTMAJOR_MASK;
    NVIC_EnableIRQ(DMA0_IRQn);

    /* start transfer */
    DMA0->SERQ = DMA_SERQ_SERQ(0);

    while(1);


}

void DMA0_IRQHandler(void)
{
    int i;
    DMA0->CINT = DMA_CINT_CINT(0);
    printf("DMA tranfer complete\r\n");
    printf("src buffer\r\n");
    for(i=0;i<sizeof(SrcBuf);i++)
    {
        printf(" %d", SrcBuf[i]);
    }
    
    printf("\r\ndest buffer\r\n");
    for(i=0;i<sizeof(DestBuf);i++)
    {
        printf(" %d", DestBuf[i]);
    }
}

/* EOF. */

